<?php
App::import('Sanitize');
class ManufacturersController extends AppController {
	var $name = 'Manufacturers';
	var $uses = array('Product', 'Manufacturer');
	var $helpers = array('Html', 'Session','Thumb');
	var $components = array('Auth','Seo');

	function beforeFilter() {
		parent::beforeFilter();
	}
	function beforeRender() {
		@$this->set('meta_title', 'Manufacturers');
		@$this->set('meta_description', 'Manufacturers');
		@$this->set('meta_keywords', 'Manufacturers');
	}

	function index() {
		$user = $this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		$manufacturers = $this->Manufacturer->find('all');
		$this->set(compact('manufacturers'));
	}

	function add() {
		$user=$this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		if(!$user || $user['User']['role'] > 0){
			$this->redirect('/');
		}
		if (!empty($this->data)) {
			$this->Manufacturer->create();
			if ($this->Manufacturer->save($this->data)) {
				$url='/manufacturers/index/';
				$this->Session->setFlash(__('The manufacturer has been saved', true));
				$this->redirect($url);
			}
			else {
				$this->Session->setFlash(__('The manufacturer could not be saved. Please, try again.', true));
			}
		}
	}

	function delete($id) {
		$user = $this->Auth->user();
		if ($user['User']['role'] == 1) {
			$this->Session->setFlash('Invalid url');
			$this->redirect('/');
		}
		$manufacturer=$this->Manufacturer->findById($id);
		$current_user=$this->Auth->user();
		if($current_user&&$current_user['User']['role']==ADMIN){
		}
		else{
			$this->Session->setFlash('Cannot delete manufacturer.');
			$this->redirect($url);
		}
		if($this->Manufacturer->delete($id)){
			$this->Session->setFlash('The manufacturer has been deleted.');
			$this->redirect(array('controller' => 'manufacturers', 'action' => 'index'));
		}
		else{
			$this->Session->setFlash('Cannot delete manufacturer.');
			$this->redirect($url);
		}
	}
}
?>
